//
// Created by axin on 24-8-12.
//
#include "seqlist.h"

//初始化
void SeqListInit(SL* ps) {
    // s.size=0;
    // s.a=NULL;
    // s.capacity=0;
    ps->a=(SlDataType*)malloc(sizeof(SlDataType)*4);
    if(ps->a==NULL) {
        printf("申请内存失败\n");
        exit(-1);
    }
    ps->size=0;
    ps->capacity=4;
}
//扩容
void SeqListCapacity(SL*ps) {
    //如果满了需要增容
    if(ps->size>=ps->capacity) {
        ps->capacity*=2;
        ps->a=(SlDataType*)realloc(ps->a,sizeof(SlDataType)*ps->capacity);
        if(ps->a==NULL) {
            printf("扩容失败\n");
            exit(-1);
        }
    }
}

//打印
void SeqListPrint(SL* ps) {
    assert(ps);
    for(int i=0;i<ps->size;i++) {
        printf("%d->",ps->a[i]);
    }
    printf("NULL\n");
    printf("ps->size=%d\n",ps->size);
    printf("ps-capacity=%d\n",ps->capacity);
}

//尾叉
void SeqListPushback(struct Seqlist*ps,SlDataType x) {
    assert(ps);
    SeqListCapacity(ps);
    ps->a[ps->size]=x;
    ps->size++;
}

//尾删
void SeqListPopBack(SL *ps) {
    assert(ps);
    ps->a[ps->size-1]=0;
    ps->size--;
}
//头茬
void SeqListPushFront(SL *ps,SlDataType x) {
    assert(ps);
    SeqListCapacity(ps);
    int end=ps->size-1;
    while(end!=-1) {
        ps->a[end+1]=ps->a[end];
        end--;
    }
    ps->a[0]=x;
    ps->size++;
}
void SeqListPopFront(SL *ps) {
    assert(ps);
    assert(ps->a);
    int start=0;
    while(start<ps->size-1) {
        ps->a[start]=ps->a[start+1];
        start++;
    }
    ps->size--;
}